<template>
  <!-- 报告单元 -->
  <div id="CAUser">
    <div class="bodyContent">
      <vxe-toolbar perfect>
        <template #buttons>
          <el-form :inline="true" :model="searchForm">
            <el-form-item label="病历号:" class='no-edit'>
              <el-input v-model="searchForm.patNo" clearable style="width: 200px" />
            </el-form-item>
            <el-form-item label="报告单元代码:" class='no-edit'>
              <el-input v-model="searchForm.rptGroup" clearable style="width: 200px" />
            </el-form-item>
            <el-form-item label="检验日期:" clearable>
              <vxe-input type="date" v-model="searchForm.startTime"></vxe-input> - <vxe-input type="date" v-model="searchForm.endTime"></vxe-input>
            </el-form-item>
          </el-form>
          <vxe-button type="text" class="button-blue" icon="vxe-icon-refresh" content="查询" @click="findCAUserList"></vxe-button>
          <vxe-button type="text" class="button-red" icon="vxe-icon-edit" content="重审" @click="auditRe"></vxe-button>
        </template>
      </vxe-toolbar>
      <div class="leftTable">
        <vxe-table ref="rptGroupTable" border="inner" align="left" class="mytable-scrollbar" :loading="showLoading" height="800" :data="cAUserList" :expand-config="{ expandAll: true, reserve: true }" :row-config="{ isCurrent: true, isHover: true }" :keyboard-config="{
                    isArrow: true,
                    isTab: true,
                    isEdit: false
                  }" :edit-config="{ trigger: 'dblclick', mode: 'row', showStatus: true }" resizable show-overflow keep-source>
          <vxe-column type="checkbox" width="30px" align="center"></vxe-column>
          <vxe-column type="seq" title="序号" width="35px" align="center"></vxe-column>
          <vxe-column field="sampleda" title="检验时间" width="120px"> </vxe-column>
          <vxe-column field="reqNo" title="条码号" width="120px"> </vxe-column>
          <vxe-column field="rptGroup" title="报告单元" width="120px"> </vxe-column>
          <vxe-column field="reqPurpose" title="申请目的" width="120px"> </vxe-column>
          <vxe-column field="patNa" title="姓名" width="120px"> </vxe-column>
          <vxe-column field="patNo" title="病历号" min-width="200px"> </vxe-column>
          <vxe-column field="testDoctor" title="检验人" min-width="130px"></vxe-column>
          <vxe-column field="testDoctorNa" title="检验人姓名" min-width="130px"></vxe-column>
          <vxe-column field="testTime" title="检验时间" min-width="130px"></vxe-column>
          <vxe-column field="chkDoctor" title="审核人" min-width="130px"></vxe-column>
          <vxe-column field="chkDoctorNa" title="审核人姓名" min-width="130px"></vxe-column>
          <vxe-column field="chkTime" title="审核时间" min-width="130px"></vxe-column>
          <vxe-column field="remark1" title="重审标记" min-width="130px"></vxe-column>
          <vxe-column field="remark2" title="重审时间" min-width="130px">
          </vxe-column>
        </vxe-table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 引入组件区 ==========
import { ref, reactive, getCurrentInstance, onMounted, watch } from 'vue';
import { funBdsJsResult } from '@/utils/Expression'; //函数公式
import { AppStore } from '@/stores/store';
import { isEmpty } from 'lodash';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { Splitpanes, Pane } from 'splitpanes';
import moment from 'moment';

// 全局变量定义区 ==========
let { proxy }: any = getCurrentInstance();
let store = AppStore();

// 获取屏幕高度
let conHeight = ref((document.documentElement.clientHeight || document.body.clientHeight) - 148 + 'px');
// 获取屏幕宽度
let conWidth = ref(document.documentElement.clientWidth);
// 加载状态
let showLoading = ref(false); //左侧表格加载+上方刷新按钮+右侧表单+右侧详情
let saveLoading = ref(false); //保存
let rptGroupTable = ref(); //报告单元表格
let cAUserList: any = reactive([]); //报告单元列表数据

// 当前选中报告单元数据
let rptGroupInfo: any = reactive({
  id: '',
  rptGroupName: ''
});

// 模块对象
let modules: any = reactive([]);
let currentUser = reactive({}); //当前报告单元

let searchForm = reactive({
  patNo: '',
  startTime: moment().format('YYYY-MM-DD'),
  endTime: moment().format('YYYY-MM-DD'),
  rptGroup: ''
});
let form = reactive({
  id: null,
  username: '',
  realname: '',
  tel: '',
  caUserId: '',
  idCard: '',
  orgId: store.userInfo.orgId,
  orgName: store.userInfo.orgName,
  activeCode: '',
  activeQrCode: '',
  signimg: '',
  seq: 0,
  interfaceId: ''
});
let interfaceTypeList = reactive([]);

let defaultProps = {
  children: 'children',
  label: 'name'
};

let sltPurpose: any = ref({});

let props = defineProps({
  systemId: String,
  moduleId: String,
  moduleSystemName: String
}) as any;
watch(
  () => props.systemId,
  (n, o) => {
    findInterfaceList();
  },
  {
    deep: true,
    immediate: true
  }
);
watch(
  () => props.moduleId,
  (newval, oldvbal) => {
    findInterfaceList();
  },
  {
    deep: true
  }
);

function findInterfaceList() {
  interfaceTypeList.length = 0;
  const url = '/api/sys/SysInterfaceCaUser/findInterfaceList';
  proxy.$http
    .get(url, { params: { systemId: props.systemId, moduleId: props.moduleId } })
    .then((res: any) => {
      if (res.data && res.data.length > 0) {
        interfaceTypeList.push(...res.data);
        form.interfaceId = res.data[0].id;
      }
      findCAUserList();
    })
    .catch((err: any) => {
      proxy.$alert(err, '提示');
    });
}

function findCAUserList() {
  showLoading.value = true;
  cAUserList.length = 0;
  const url = '/api/sys/SysInterfaceCaUser/findIsChkMainInfoByPatNo';
  proxy.$http
    .get(url, { params: { patNo: searchForm.patNo, startTime: searchForm.startTime + ' 00:00:00', endTime: searchForm.endTime + ' 23:59:59', rptGroup: searchForm.rptGroup } })
    .then((res: any) => {
      cAUserList.push(...res.data);
      showLoading.value = false;
    })
    .catch((err: any) => {
      proxy.$alert(err, '提示');
    });
  //showLoading.value = true;
}

// 清空右侧表单
function clearForm() {
  form.id = null;
  form.tel = '';
  form.caUserId = '';
  form.idCard = '';
  form.activeCode = '';
  form.signimg = '';
  form.activeQrCode = '';
}

function auditRe() {
  const checkedBoxRow = proxy.$refs.rptGroupTable.getCheckboxRecords();
  if (!(checkedBoxRow.length > 0)) {
    VxeMessage('请选择要重审的数据', VxeNotifyType.error);
  } else {
    showLoading.value = true;
    const ids = [];
    checkedBoxRow.forEach((item) => {
      ids.push(item.id);
    });
    proxy.$http
      .post('/api/sys/SysInterfaceCaUser/auditRe', ids)
      .then((res: any) => {
        if (res && res.status && res.status.code !== 1) {
          showLoading.value = false;
          VxeMessage(res.status.msg, VxeNotifyType.error);
        } else if (res && res.status && res.status.code === 1) {
          showLoading.value = false;
          VxeMessage('重审成功', VxeNotifyType.success);
          findCAUserList();
        } else {
          showLoading.value = false;
          VxeMessage('请求错误', VxeNotifyType.error);
        }
      })
      .catch((err: any) => {
        showLoading.value = false;
        VxeMessage('重审失败', VxeNotifyType.error);
      });
  }
}
</script>

<style lang="scss" scoped>
#CAUser {
  height: 100%;

  .bodyContent {
    height: calc(100% - 60px);
    background-color: $base-white;

    // 附加按钮间距
    .button-blue,
    .button-red {
      margin: 5px 0 5px 5px;
    }

    .tableBody {
      height: calc(100% - 40px);
    }

    .el-tabs {
      height: 100%;

      :deep(.el-tabs__content) {
        padding: 0;
        height: calc(100% - 39px);

        .el-tab-pane {
          height: 100%;
        }

        #pane-first {
          .el-form {
            padding: 20px;
          }
        }
      }
    }

    // 各个组件与右侧删除按钮的间距
    .vxe-checkbox {
      margin: 5px;
    }
  }
}
</style>
